// pages/addCommunity/addCommunity.js
const app = getApp();
import api from '../../../../utils/util.js'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    statusBarHeight: app.globalData.statusBarHeight,
    fullHeight: app.globalData.fullcontain,
    navmess: {
      navtitle: "新增小区",
      backhide: 0,
      topheight: 1
    },
    index1: -1,
    index2: -1,
    index3: -1,
    provinceArr: [],
    cityArr: [],
    regionArr: []
  },
  pageto: function (e) {
    var url = e.currentTarget.dataset.url,
      data = e.currentTarget.dataset.datobj;
    app.goto(url, data);
  },
  // 省市区picker获取
  bindPickerChange1: function (e) {
    var that = this;
    console.log('省份', e.detail.value)
    this.setData({
      index1: e.detail.value,
      provinceId: this.data.provinceArr[e.detail.value].id,
    })
    api.post("/common/regionList", {
      "pid": that.data.provinceId
    }).then(res => {
      console.log("城市", res)
      that.setData({
        cityArr: res.result,
        index2: 0,
        cityId: res.result[0].id
      })
      that.getArea(that);
    })
  },
  bindPickerChange2: function (e) {
    var that = this;
    console.log('城市', e.detail.value)
    this.setData({
      index2: e.detail.value,
      cityId: this.data.cityArr[e.detail.value].id
    })
    api.post("/common/regionList", {
      "pid": that.data.cityId
    }).then(res => {
      console.log("区域", res)
      that.setData({
        regionArr: res.result,
        index3: 0,
        regionId: res.result[0].id
      })
    })
  },
  bindPickerChange3: function (e) {
    var that = this;
    console.log('区域', e.detail.value)
    var that = this;
    this.setData({
      index3: e.detail.value,
      regionId: this.data.regionArr[e.detail.value].id
    })
  },
  checkNull: function(){
    if(!this.data.cityArr.length){
      wx.showToast({
        title: '请选择省份',
        icon: "none",
        duration: 1500
      })
    }
  },
  // 填写地址
  inputtxt: function(e){
    this.setData({
      detail_info: e.detail.value
    })
  },
  // 移动选点
  onChangeAddress: function () {
    var that = this;
    wx.chooseLocation({
      success: function (res) {
        console.log(res)
        that.setData({
          jd: res.longitude,
          wd: res.latitude
        })
        // 地址解析
        wx.request({
          url: 'https://apis.map.qq.com/ws/geocoder/v1/',
          method: "GET",
          data:{
            "address": res.address,
            "key": "GIDBZ-7QGK4-BVZUU-XEDJD-AYKCT-D4BCA"
          },
          success: function(res){
            console.log(res)
          }
        })
        // 逆地址解析 根据经纬度获取省市区
        // wx.getLocation({
        //   success: function (res) {
        //     var jd = that.data.jd,
        //         wd = that.data.wd;
        //     wx.request({
        //       url: 'https://apis.map.qq.com/ws/geocoder/v1/?location=' +wd+ ','+jd+ '&key=GIDBZ-7QGK4-BVZUU-XEDJD-AYKCT-D4BCA',
        //       success: function (res) {
        //         console.log(res)
        //       }
        //     })
        //   },
        // })
      },
      fail: function (err) {
        console.log(err)
      }
    });
  },
  // 新增小区保存
  savebtn: function(){
    var that = this;
    var provinceId = this.data.provinceId,
        cityId = this.data.cityId,
        regionId = this.data.regionId,
        detail_info= this.data.detail_info,
        token = this.data.token;
    if(provinceId&&cityId&&regionId&&detail_info){
      console.log(provinceId,cityId,regionId,detail_info)
      api.post("/mini/my/basic/addSmallArea",{
        "token": token,
        "provinceId": provinceId,
        "cityId": cityId,
        "areaId": regionId,
        "name": detail_info
      }).then(res => {
        console.log(res)
        if(res.result){
          wx.showToast({
            title: '保存成功',
            icon: "success",
            duration: 1500
          })
          setTimeout(function(){
            wx.navigateBack({
              delta: 1,
            })
          },1500)
        }else{
          wx.showToast({
            title: '保存失败',
            icon: "none",
            duration: 1500
          })
        }
      }).catch(res => {
        console.log(res)
      })
    }else{
      wx.showToast({
        title: '请完整填写小区信息',
        icon: "none",
        duration: 1500
      })
    }

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var token = wx.getStorageSync('token');
    this.setData({
      token: token
    })
    api.post("/common/regionList", {
      "pid": 100000
    }).then(res => {
      console.log("省份", res)
      that.setData({
        provinceArr: res.result
      })
    })
  },
  getCity: function (self) {
    api.post("/common/regionList", {
      "pid": self.data.provinceId
    }).then(res => {
      console.log("城市", res)
      self.setData({
        cityArr: res.result,
        index2: 0,
        cityId: res.result[0].id
      })
      self.getArea(self);
    })
  },
  getArea: function (other) {
    api.post("/common/regionList", {
      "pid": other.data.cityId
    }).then(res => {
      console.log("区域", res)
      other.setData({
        regionArr: res.result,
        index3: 0,
        regionId: res.result[0].id
      })
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onReady: function (e) {

  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})